{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Python基础教程 高级数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "列表\n",
      "8.0\n"
     ]
    }
   ],
   "source": [
    "#列表  箱单莫过于C++中的数组 只不过Python中的列表可以存放不同数据类型的变量\n",
    "#创建列表直接使用中括号[]即可\n",
    "lst1=[3,4,5,6,\"列表\",8.0]\n",
    "print(type(lst1))\n",
    "\n",
    "#我们使用for循环再来遍历一下这个列表  Python中的列表也支持索引操作\n",
    "for i in range(len(lst1)):\n",
    "    print(lst1[i])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8.0\n"
     ]
    }
   ],
   "source": [
    "#python中的列表支持逆序索引操作\n",
    "print(lst1[-1])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "#列表切片\n",
    "#listName[start:end[:step]  start表示起始索引（默认为0） end为结束索引 step为步长\n",
    "print(lst1[:3])     #取前三个元素 括号里面的3代表end的数值 区间是前闭后开  [0,3)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 6, '列表']\n"
     ]
    }
   ],
   "source": [
    "print(lst1[2:5])    #从第三个元素开始，连续取3个元素"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 6, '列表', 8.0]\n"
     ]
    }
   ],
   "source": [
    "print(lst1[2:]) #取第三个元素及其后的所有元素"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[8.0, '列表', 6, 5, 4, 3]\n"
     ]
    }
   ],
   "source": [
    "print(lst1[::-1])   #将列表倒过来"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 5, '列表']\n"
     ]
    }
   ],
   "source": [
    "print(lst1[::2])        #隔一个取一个 理解步长的意义  为什么这里步长填的是2而不是1？"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, '列表']\n"
     ]
    }
   ],
   "source": [
    "print(lst1[1::3])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['a', 'b', 'c', 'd'], [1, 2, 3, 4]]\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "#Python中也有高维数组一说\n",
    "a=['a','b','c','d']\n",
    "b=[1,2,3,4]\n",
    "c=[a,b] #高维就是容纳了多个低维\n",
    "print(c)\n",
    "print(c[0][0])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "大一|大二|大三|大四|\n",
      "0 大一\n",
      "1 大二\n",
      "2 大三\n",
      "3 大四\n"
     ]
    }
   ],
   "source": [
    "lst2=[\"大一\",\"大二\",\"大三\",\"大四\"]\n",
    "\n",
    "#遍历列表 使用|分割\n",
    "for item in lst2:\n",
    "    print(item,end='|')\n",
    "print() #换行\n",
    "\n",
    "#遍历列表，同时显示索引号\n",
    "for index,item in enumerate(lst2):\n",
    "    print(index,item)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 列表的增删改基本操作"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'C++', 'Ruby', 'Go', 'Java']\n"
     ]
    }
   ],
   "source": [
    "language=[\"Python\",\"C++\",\"Ruby\",\"Go\"]\n",
    "\n",
    "#添加元素 Java\n",
    "language.append(\"Java\")\n",
    "print(language)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'C++', 'VB', 'VB', 'Ruby', 'Go', 'Java']\n"
     ]
    }
   ],
   "source": [
    "#在索引为2的位置添加元素 VB\n",
    "language.insert(2,\"VB\")\n",
    "print(language)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [],
   "source": [
    "language2=[\"Chinese\",\"English\"]\n",
    "#将language2添加到language的末尾\n",
    "language.extend(language2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'C++', 'VB', 'VB', 'Ruby', 'Go', 'Java', 'Chinese', 'English']\n"
     ]
    }
   ],
   "source": [
    "print((language))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 5, 7, 3, 9, 11]\n",
      "[3, 7, 3, 9, 11]\n",
      "[3, 3, 9, 11]\n",
      "7\n",
      "[3, 3, 9]\n",
      "[3, 9]\n"
     ]
    }
   ],
   "source": [
    "#创建一个列表，先创建一个空列表，然后使用for循环往列表中添加元素\n",
    "#创建一个空列表\n",
    "lst4=[]\n",
    "#利用for循环，根据一个列表，创建一个新列表lst4\n",
    "for i in [1,2,3,1,4,5]:\n",
    "    lst4.append(i*2+1)\n",
    "print(lst4)\n",
    "\n",
    "#删除索引为1的元素\n",
    "del lst4[1]   #直接从列表中删除，不返回删除的元素\n",
    "print(lst4)\n",
    "#删除索引为1的元素，并返回被删除的元素\n",
    "item=lst4.pop(1)\n",
    "print(lst4)\n",
    "print(item)\n",
    "#pop不指定索引位置，将删除列表最后一个元素\n",
    "lst4.pop()\n",
    "print(lst4)\n",
    "\n",
    "##根据值(或列表元素)来删除\n",
    "lst4= [3, 3, 9]\n",
    "lst4.remove(3)  #删除第一个匹配的值，大家可思考一下如何删除相同值？\n",
    "print(lst4)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [],
   "source": [
    "#修改元素就不做赘述"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 列表常用方法"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100 65\n"
     ]
    }
   ],
   "source": [
    "#假设列表lst6记录某学科的成绩\n",
    "lst6=[98,97,87,88,76,68,80,100,65,87,84,92,95,87]\n",
    "#求列表lst6的最大值、最小值\n",
    "print(max(lst6),min(lst6)) #100,65"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1204\n",
      "86.0\n"
     ]
    }
   ],
   "source": [
    "#求列表lst6所有元素总和\n",
    "print(sum(lst6))\n",
    "#求列表lst6所有元素的平均值\n",
    "print(sum(lst6)/len(lst6))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "#求列表lst6中87分数重复出现的次数\n",
    "print(lst6.count(87))\n",
    "#求87第一次出现的索引\n",
    "print(lst6.index(87))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表V1的元素个数: 7\n",
      "列表D1的元素个数: 3\n",
      "列表D1第1个元素的元素个数: 4\n"
     ]
    }
   ],
   "source": [
    "#元素都是数字的情况\n",
    "V1=[0.1,2,0.4,9,10,-1,-5.5]\n",
    "print(\"列表V1的元素个数:\",len(V1))#结果为7\n",
    "#元素是列表的情况\n",
    "D1=[[1,2,3,4],[5,6,7,8],[9,10,11,12]]\n",
    "print(\"列表D1的元素个数:\",len(D1))  #结果是3，而不是12！\n",
    "#D1的第一个元素是列表[1,2,3,4]的元素个数\n",
    "print(\"列表D1第1个元素的元素个数:\",len(D1[0]))#4"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "[65, 68, 76, 80, 84, 87, 87, 87, 88, 92, 95, 97, 98, 100]\n",
      "None\n",
      "['gaoqiu', 'liujing', 'wangbo', 'zhangfei']\n"
     ]
    }
   ],
   "source": [
    "#列表元素为数字##########\n",
    "lst7=[98,97,87,88,76,68,80,100,65,87,84,92,95,87]\n",
    "#按数字大小升序排序\n",
    "print(lst7.sort(reverse=False))\n",
    "#显示列表\n",
    "print(lst7)\n",
    "#列表元素为字符串##########\n",
    "lst8=[\"zhangfei\",\"liujing\",\"gaoqiu\",\"wangbo\"]\n",
    "#直接修改列表，返回None值，按字母升序排序\n",
    "print(lst8.sort(reverse=False))\n",
    "#显示列表\n",
    "print(lst8)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 元组：不可以改动的列表"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "#元组使用()顶八个亿\n",
    "t1=(1,2,3)\n",
    "print(type(t1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "#生成元组\n",
    "#用list()函数可以把range生成的序列转换为列表，与此类似可以用tuple()函数把range生成的序列转换为元组，也可用tuple()函数把列表转换为元组。\n",
    "a1=tuple(range(5))\n",
    "#查看a1的类型\n",
    "print(type(a1))\n",
    "lst=[\"华为\",\"苹果\",\"三星\"]\n",
    "t1=tuple(lst)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "参数：\n",
      "python的版本(2.7, 3.6, 3.7, 3.8)\n"
     ]
    }
   ],
   "source": [
    "#元组在可变参数中的使用\n",
    "def test01(name,*args):\n",
    "    print(\"参数：\")\n",
    "    print(\"{}的版本{}\".format(name,args))\n",
    "\n",
    "test01('python',2.7,3.6,3.7,3.8)  #Python后台实际把版本这些数装包成一个元祖"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "函数返回值: ('python', 'numpy', 'pytorch')\n",
      "python numpy pytorch\n"
     ]
    }
   ],
   "source": [
    "#定义一个返回多值函数\n",
    "def return_mul():\n",
    "    return 'python','numpy','pytorch'\n",
    "\n",
    "res=return_mul()\n",
    "print(\"函数返回值:\",res)\n",
    "#函数返回值为一个元祖，对这个元祖解包\n",
    "a,b,c=return_mul()\n",
    "print(a,b,c)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 字典"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict1的类型: <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "#字典相当于C++中的哈希表\n",
    "dict1={'小猫':1,'小狗':2,'黄牛':3,'水牛':3 ,'羊':4}\n",
    "print(\"dict1的类型:\",type(dict1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "字典元素的“名字”叫键，“内容”叫值，因此字典的每个元素都是键-值对。\n",
    "请注意！键必须是不可变数据类型：字符串，元组等，值的类型是随意的，你甚至可以写个None（空对象关键字）"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 字典的CURD(增删查改)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{19: 'one'}\n",
      "{19: 'one', 'two': 20, 'one': 1}\n"
     ]
    }
   ],
   "source": [
    "dict2={}\n",
    "\n",
    "#添加键值对 lyx-18\n",
    "dict2[19]=\"one\"\n",
    "print(dict2)\n",
    "\n",
    "dict2[\"two\"]=20\n",
    "dict2[\"one\"]=1\n",
    "print(dict2)\n",
    "#字典中 前面为key 后面为value"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "outputs": [],
   "source": [
    "#修改键值对\n",
    "dict2['one']=2"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{19: 'one', 'two': 20, 'one': 2}\n"
     ]
    }
   ],
   "source": [
    "print(dict2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "outputs": [],
   "source": [
    "#删除键值对\n",
    "# 删除某一个键值对 del为删除对象关键字，直接从内存层面抹去\n",
    "del dict2[\"one\"]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{19: 'one', 'two': 20}\n"
     ]
    }
   ],
   "source": [
    "print(dict2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "#全部清空\n",
    "dict2.clear()\n",
    "print(dict2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "outputs": [],
   "source": [
    "#删除字典 并释放内存\n",
    "del dict2"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'dict2' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[1;32mC:\\Users\\ADMINI~1\\AppData\\Local\\Temp/ipykernel_15204/738239332.py\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[1;32m----> 1\u001B[1;33m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mdict2\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m",
      "\u001B[1;31mNameError\u001B[0m: name 'dict2' is not defined"
     ]
    }
   ],
   "source": [
    "print(dict2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 遍历字典"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "字典值 :dict_items([('Google', 'www.google.com'), ('baidu', 'www.baidu.com'), ('taobao', 'www.taobao.com')]) \n",
      "Google www.google.com\n",
      "baidu www.baidu.com\n",
      "taobao www.taobao.com\n"
     ]
    }
   ],
   "source": [
    "#创建一个字典\n",
    "dict55 = {'Google': 'www.google.com', 'baidu': 'www.baidu.com', 'taobao': 'www.taobao.com'}\n",
    "#打印字典的键-值对 items会返回一个类似元组列表的可循环对象（可迭代对象）\n",
    "print(\"字典值 :%s \"%dict55.items())\n",
    "#字典值 :dict_items([('Google', 'www.google.com'), ('baidu', 'www.baidu.com'), ('taobao', 'www.taobao.com')])\n",
    "#遍历字典的所有键-值对\n",
    "for key,value in dict55.items():\n",
    "    print(key,value)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w1\n",
      "w2\n",
      "w3\n",
      "w1\n",
      "w2\n",
      "w3\n"
     ]
    }
   ],
   "source": [
    "#单独遍历键\n",
    "# 生成一个字典\n",
    "dict56={'w1':[1,2,3,4],'w2':[5,6,7,8],'w3':[9,10,11,12]}\n",
    "#直接遍历字典名获得的是键\n",
    "for key in dict56:\n",
    "    print(key)\n",
    "#遍历字典函数keys()的值,其结果与直接遍历字典名相同\n",
    "for key in dict56.keys():\n",
    "    print(key)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "[5, 6, 7, 8]\n",
      "[9, 10, 11, 12]\n"
     ]
    }
   ],
   "source": [
    "#单独遍历值\n",
    "for value in dict56.values():\n",
    "    print(value)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'one': 1, 'two': 2, 'three': 3}\n",
      "a: {'one': 11, 'two': 2, 'three': 3}\n",
      "{'one': 1, 'two': 2, 'three': 3}\n"
     ]
    }
   ],
   "source": [
    "a = {'one': 1, 'two': 2, 'three': 3}\n",
    "b = a.copy()\n",
    "print(b)\n",
    "a['one']=11\n",
    "print(\"a:\",a)\n",
    "print((b))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'a' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[1;32mC:\\Users\\ADMINI~1\\AppData\\Local\\Temp/ipykernel_15204/660662506.py\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[1;32mdel\u001B[0m \u001B[0ma\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m----> 2\u001B[1;33m \u001B[0ma\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m",
      "\u001B[1;31mNameError\u001B[0m: name 'a' is not defined"
     ]
    }
   ],
   "source": [
    "del a\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "outputs": [
    {
     "data": {
      "text/plain": "{'one': 1, 'two': 2, 'three': 3}"
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 集合"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "set51={1,2,3,4,5,6}\n",
    "\n",
    "#不能用这种方法创建空集合，以下创建的是空字典\n",
    "set52={} #字典\n",
    "print(type(set52))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5, 6, 7, 8, 9}\n",
      "set()\n"
     ]
    }
   ],
   "source": [
    "#set方法\n",
    "lst51=[1,2,3,4,5,5,6,6,7,8,9]\n",
    "#使用set()函数创建集合\n",
    "set53=set(lst51)\n",
    "print(set53) #{1, 2, 3, 4, 5, 6, 7, 8, 9}\n",
    "#创建空集合\n",
    "set54=set()\n",
    "print(set54)  # set()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'set'>\n"
     ]
    }
   ],
   "source": [
    "print(type(set53))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 2, 3, 4}\n",
      "{1, 2, 3}\n"
     ]
    }
   ],
   "source": [
    "#创建一个空集合\n",
    "set54=set()\n",
    "#往集合中添加元素\n",
    "for i in range(5):\n",
    "    set54.add(i)\n",
    "print(set54)\n",
    "\n",
    "#定义动物和标签构成的字典\n",
    "dict57={'白猫':1,'黑猫':1,'狼狗':2,'哈巴狗':2,'小麻雀':3,'打麻雀':3}\n",
    "#把标签放在一个集合中，实现自动去重\n",
    "set55=set()\n",
    "for value in dict57.values():\n",
    "    set55.add(value)\n",
    "print(set55)   #{1, 2, 3}"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 2, 3, 4}\n",
      "set()\n"
     ]
    }
   ],
   "source": [
    "#remove删除不存在的元素，将报错，所以先判断，然后再删除\n",
    "i=10\n",
    "if i in set54:\n",
    "    set54.remove(i)\n",
    "print(set54)\n",
    "#使用discard，功能和remove一样，好处是没有的话，不会报错\n",
    "set54.discard(10)\n",
    "\n",
    "#用pop删除，在list里默认删除最后一个，在set里随机删除一个\n",
    "set54.pop()\n",
    "#清除所有元素\n",
    "set54.clear()\n",
    "print(set54)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}